<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>doregister</title>
    <link rel="stylesheet" href="/static/lib/css/layui.css" media="all">
</head>

<body>

    <blockquote class="layui-elem-quote layui-text">
        注意: 必须首先指定某个班级 每次签到以创建时间区分
    </blockquote>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend>进行签到</legend>
    </fieldset>

    <form class="layui-form layui-form-pane" action="" lay-filter="">

        <div class="layui-form-item">
            <label class="layui-form-label">选择班级</label>
            <div class="layui-input-inline">
                <select name="classname" lay-verify="required" lay-search="" lay-filter="chooseclass" id='chooseclass'>
                    <option value="">请联系老师加入新班级</option>
                    {% for class in classes %}
                    <option value="{{ class.id }}">{{ class.classname }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="demoTable" style="margin-top: 15px;">
                <table class="layui-hide" id="LAY_table_user" lay-filter="LAY_table_user"></table>
            </div>
        </div>

    </form>
    <script type="text/html" id="barDemo">
      <a class="layui-btn layui-btn-xs" lay-event="do">立即签到</a>
    </script>
    <script src="/static/lib/layui.js" charset="utf-8"></script>
    <script src="/static/lib/axios.min.js"></script>
    <script>
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate
                , $ = layui.$
                , table = layui.table;

            $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            form.on('select(chooseclass)', function (data) {
                console.log(data.value); //得到被选中的值
                classid = data.value
                table.render({
                    elem: '#LAY_table_user'
                    , url: '../api/register?classid='+classid+'&result=未签到'
                    , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                    , defaultToolbar: ['filter', 'exports', 'print' ]
                    , cols: [[
                        { field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left' }
                        , { field: 'student_id', title: '学生id', width: 95, sort: true }
                        , { field: 'class_id', title: '班级id', width: 95, sort: true }
                        , { field: 'create_time', title: '发起时间', sort: true, width: 160 }
                        , { field: 'time', title: '签到时间', sort: true, width: 160 }
                        , { field: 'result', title: '结果', width: 90, fixed: 'right', sort: true }
                        , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 90 }
                    ]]
                    , page: true
                    , id: 'reload_table'
                    , height: 'ful-400'
                });
                //监听行工具事件
                table.on('tool(LAY_table_user)', function (obj) {
                    var data = obj.data;
                    // console.log(data)
                    if (obj.event === 'do') {
                        /* layer.confirm('真的删除行么', function (index) {
                            obj.del();
                            layer.close(index);
                        }); */
                        formData = new FormData()
                        formData.append("registerid",data.id)
                        axios({
                            method: 'post',
                            url: '../api/doregister',
                            data: formData
                        })
                            .then(function (response) {
                                console.log(response.data);
                                result = response.data.result
                                if (result == "wrong-student") {
                                    layer.msg("签到失败")
                                } else if (result == "register-done") {
                                    layer.msg("签到成功")
                                }
                            })
                            .catch(function (error) {
                                console.log(error);
                            });
                    }
                });
            });

        });
    </script>

</body>

</html>